﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.IO;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;


namespace PRE
{
    public partial class frmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        DAL.Entities_DAL.TaikhoanDAL _dal = new DAL.Entities_DAL.TaikhoanDAL();
        BLL.Entities_BLL.TaikhoanBLL _bll = new BLL.Entities_BLL.TaikhoanBLL();
        public Dictionary<Type, Form> OpenForms = new Dictionary<Type, Form>();

        private const string STR_LOGIN = "Đăng &nhập";
        private const string STR_LOGOUT = "Đăng &xuất";

        public static UTL.Session _sss = new UTL.Session();

        public frmMain()
        {
            InitializeComponent();

            //load gio dien ngau nhien
            //System.Random r = new Random();
            //DevExpress.Skins.SkinContainerCollection skinCollection = DevExpress.Skins.SkinManager.Default.Skins;
            //DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinCollection[r.Next(0, skinCollection.Count)].SkinName);
        }
        //doi pass
        public string luu;
        //
       System.Random r = new Random();           


        private void bbiDangnhap_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (bbiDangnhap.Caption == STR_LOGOUT)
            {
                _sss.Login = false;
                bbiDangnhap.Caption = STR_LOGIN;
                CloseAll_TabPages();
            }

            var x = new Catalog.frmLogin();
            x.ShowDialog();
           
            //dn thanh cong hien len het
            if (_sss.Login)
            {
                bsiUser.Caption = _sss.Name;
                user.Text = _sss.Name;
                txtTen.Text = _sss.Acc;
                role.Text = _sss.Role + "";
                mann.Caption = _sss.Id;
                bbiDangnhap.Caption = STR_LOGOUT;

                rpTC.Visible = true;
                rpgND.Visible = true;
                rpTKe.Visible = true;
                rpQLDM.Visible = true;
                rpQuanly.Visible = true;
                rpgPMT.Visible = true;
                bbiTaikhoan.Visibility = BarItemVisibility.Always;
                bbiDMK.Visibility = BarItemVisibility.Always;
            }

            // nguoc lai thi an het
            else
            {
                bsiUser.Caption = _sss.Name;
                txtTen.Text = _sss.Name;
                user.Text = "Tên Nhân Viên";
                role.Text = "None";
                rpgND.Visible = false;
                rpTKe.Visible = false;
                rpQLDM.Visible = false;
                rpQuanly.Visible = false;
                rpgND.Visible = false;
                //bbiPhanquyen.Visibility = BarItemVisibility.Never;
                bbiTaikhoan.Visibility = BarItemVisibility.Never;
                bbiDMK.Visibility = BarItemVisibility.Never;
            }

            EnableMenu();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            bbiDangnhap_ItemClick(sender, null);
            ShowMdiChildForm<Catalog.frmLoad>();
        }
        //phan quyen
        private void EnableMenu()
        {
            switch (_sss.Role)
            {
                //toan quyen
                case UTL.Session.Roles.Administrator:
                    rpQLDM.Visible = true;
                    rpgND.Visible = true;
                    rpTKe.Visible = true;
                    rpgPMT.Visible = true;
                    bbiTaikhoan.Visibility = BarItemVisibility.Always;
                    bbiNhanvien.Visibility = BarItemVisibility.Always;
                    bbiDMK.Visibility = BarItemVisibility.Always;

                    break;

                //quyen quan ly
                case UTL.Session.Roles.Manager:
                    rpgND.Visible = true;
                    rpTKe.Visible = true;
                    rpgND.Visible = false;

                    break;

                // quyen nhan vien
                case UTL.Session.Roles.Staff:
                    rpQLDM.Visible = true;
                    rpgND.Visible = true;
                    rpTKe.Visible = false;
                    rpgPMT.Visible = true;
                    rpgND.Visible = false;
                    bbiBGN.Visibility = BarItemVisibility.Never;

                    break;

                // quyen nguoi dung
                case UTL.Session.Roles.User:
                    rpQLDM.Visible = true;
                    rpgND.Visible = false;
                    rpTKe.Visible = false;
                    rpgPMT.Visible = false;
                    //bbiPhanquyen.Visibility = BarItemVisibility.Never;
                    bbiTaikhoan.Visibility = BarItemVisibility.Never;
                    bbiNhanvien.Visibility = BarItemVisibility.Never;
                    bbiBGN.Visibility = BarItemVisibility.Never;
                 
                    break;

                default:
                    break;
            }
        }

        private void bbiTaikhoan_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowMdiChildForm<Catalog.frmTaikhoan>();
        }

        private void bbiNhanvien_ItemClick(object sender, ItemClickEventArgs e)
        {
            
            ShowMdiChildForm<Catalog.frmNhanvien>();
        }

        private void bbiCanho_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.frmPhong) return;
            var frm = new Catalog.frmPhong() { MdiParent = this };
            frm.Sender(mann.Caption);
            frm.Show();
        }

        private void bbiChunhcu_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowMdiChildForm < Catalog.frmLoaiphong>();
        }

        private void bbiBGN_ItemClick(object sender, ItemClickEventArgs e)
        {            
            ShowMdiChildForm<Catalog.frmNCC>();
        }

        private void bbiBCSN_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.frmThietbi) return;
            var frm = new Catalog.frmThietbi() { MdiParent = this };         
            frm.Show();
        }

        private void bbiDMK_ItemClick(object sender, ItemClickEventArgs e)
        {
            //--gọi from doimatkhau lên
            PRE.Catalog.frmDoimatkhau Child = new Catalog.frmDoimatkhau();

            //--- truyền tham số linklabel1 qua from đổi mk
            Child.Sender(txtTen.Text);
            //---hien thi from dmk
            Child.Show();
        }

        //dong form hien tai khi mo form moi
        public void ShowMdiChildForm<T>(bool closeOpentabs = true) where T : Form, new()
        {
            Form instance;
            OpenForms.TryGetValue(typeof(T), out instance);
            if (instance == null || instance.IsDisposed)
            {
                if (closeOpentabs)
                {
                    CloseAll_TabPages();
                }
                instance = new T();
                OpenForms[typeof(T)] = instance;
                instance.MdiParent = this;
                instance.Show();
            }
            else
            {
                instance.Activate();
            }
        }
        private void CloseAll_TabPages()
        {
            foreach (var item in MdiChildren)
            {
                item.Close();
            }

        }

        private void bbiKhuvuc_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowMdiChildForm<Catalog.frmKhuvuc>();
        }

        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowMdiChildForm<Catalog.frmLTB>();
        }

        private void bbiCH_ItemClick(object sender, ItemClickEventArgs e)
        {
            //ShowMdiChildForm<frmKetnoiCSDL>();
        }

        private void bbiDTCC_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowMdiChildForm<Catalog.frmThongke>();
        }

        private void bbiLHD_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowMdiChildForm<Catalog.frmPhieumuon>();
        }

        private void bbiPT_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowMdiChildForm<Catalog.frmPhieutra>();
        }

        private void bbiNV_CC_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowMdiChildForm<Catalog.frmQLTB>();
        }

        private void bbiHDtiennha_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowMdiChildForm<Catalog.frmDoituong>();
        }

    }
}